package com.abe.libquick.widget.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.abe.libquick.R;
import com.abe.libquick.utils.NormalUtils;
import com.abe.libquick.utils.SoftKeyboardHelper;
import com.xw.repo.XEditText;

public class FormEditText extends RelativeLayout implements View.OnClickListener, XEditText.OnXFocusChangeListener {
    private ImageView itemRequiredImg;
    private TextView itemLabelName;
    private TextView itemLabelNameFocus;
    private XEditText itemValue;
    private boolean required, canEdit;
    private String label, value;
    private RequiredListener listener;
    private int inputType;
    private int maxLength;

    public FormEditText(Context context) {
        this(context, null);
    }

    public FormEditText(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FormEditText(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        //抽取需要的自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.FormEditText);
        maxLength = typedArray.getInt(R.styleable.FormEditText_maxLength, -1);
        maxLength = maxLength > 0 ? maxLength : Integer.MAX_VALUE;
        int inputTypeCode = typedArray.getInt(R.styleable.FormEditText_inputType, 0);
        int labelRes = typedArray.getResourceId(R.styleable.FormEditText_labelRes, 0);
        String labelStr = typedArray.getString(R.styleable.FormEditText_label);
        int valueRes = typedArray.getResourceId(R.styleable.FormEditText_valueRes, 0);
        String valueStr = typedArray.getString(R.styleable.FormEditText_value);
        if (labelRes != 0) {
            label = getContext().getResources().getString(labelRes);
        }
        if (!TextUtils.isEmpty(labelStr)) {
            label = labelStr;
        }
        if (valueRes != 0) {
            value = getContext().getResources().getString(valueRes);
        }
        if (!TextUtils.isEmpty(valueStr)) {
            value = valueStr;
        }
        required = typedArray.getBoolean(R.styleable.FormEditText_required, false);
        boolean canEdit = typedArray.getBoolean(R.styleable.FormEditText_canEdit, true);
        float labelSmallSize = typedArray.getDimension(R.styleable.FormEditText_labelSmallSize, NormalUtils.dip2px(getContext(), 12));
        float labelSize = typedArray.getDimension(R.styleable.FormEditText_labelSize, NormalUtils.dip2px(getContext(), 16));
        float valueSize = typedArray.getDimension(R.styleable.FormEditText_valueSize, NormalUtils.dip2px(getContext(), 14));
        typedArray.recycle();
        //组合View
        View view = View.inflate(context, R.layout.item_abe_view_common_form_edit, this);
        itemRequiredImg = view.findViewById(R.id.item_required_img);
        itemLabelName = view.findViewById(R.id.item_label_name);
        itemLabelNameFocus = view.findViewById(R.id.item_label_name_focus);
        itemValue = view.findViewById(R.id.item_value);
        itemValue.setOnXFocusChangeListener(this);
        itemLabelName.setText(label);
        itemLabelNameFocus.setText(label);
        itemValue.setText(value);
        itemLabelName.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelSize);
        itemLabelNameFocus.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelSmallSize);
        itemValue.setTextSize(TypedValue.COMPLEX_UNIT_PX, valueSize);
        switch (inputTypeCode) {
            case 1:
                inputType = InputType.TYPE_CLASS_PHONE;
                break;
            case 2:
                inputType = InputType.TYPE_CLASS_NUMBER;
                break;
            case 3:
                inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD;
                break;
            case 4:
                inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL;
                break;
            default:
                inputType = InputType.TYPE_CLASS_TEXT;
                break;
        }
        itemValue.setInputType(inputType);
        setCanEdit(canEdit);
        itemRequiredImg.setBackgroundResource(required ? R.mipmap.icon_required : R.color.translate);
        setOnClickListener(this);
    }

    public void init(String label, String value, boolean required) {
        setLabel(label);
        setValue(value);
        setRequired(required);
    }

    public XEditText getItemValue() {
        return itemValue;
    }

    public void setListener(RequiredListener listener) {
        this.listener = listener;
    }

    public void setRequired(boolean required) {
        this.required = required;
        if (itemRequiredImg != null) {
            itemRequiredImg.setBackgroundResource(required ? R.mipmap.icon_required : R.color.translate);
        }
    }

    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength > 0 ? maxLength : Integer.MAX_VALUE;
        if (itemValue != null) {
            itemValue.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        }
    }

    public void setLabel(String label) {
        this.label = label;
        if (itemLabelName != null) {
            itemLabelName.setText(label);
        }
        if (itemLabelNameFocus != null) {
            itemLabelNameFocus.setText(label);
        }
    }

    public void setLabelRes(int labelRes) {
        setLabel(getContext().getResources().getString(labelRes));
    }

    public void setValue(String value) {
        this.value = value;
        if (itemValue != null) {
            itemValue.setText(value);
        }
    }

    public void setValueRes(int valueRes) {
        setValue(getContext().getResources().getString(valueRes));
    }

    public String getText() {
        return itemValue.getText() == null ? "" : itemValue.getText().toString().trim();
    }

    public boolean required() {
        if (required && listener != null) {
            listener.onRequired(label);
        }
        return required;
    }

    public void setInputType(int inputType) {
        this.inputType = inputType;
        if (itemValue != null) {
            itemValue.setInputType(inputType);
        }
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
        if (itemValue != null) {
            if (canEdit) {
                itemValue.setFocusable(true);
                itemValue.setFocusableInTouchMode(true);
                itemValue.requestFocus();
                SoftKeyboardHelper.show(getContext(), itemValue);
            } else {
                itemValue.setFocusable(false);
                itemValue.setFocusableInTouchMode(false);
            }
        }
    }

    @Override
    public void onClick(View v) {
        setCanEdit(canEdit);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (itemValue == null) return;
        int width = getWidth() - NormalUtils.dip2px(getContext(), 25);
        if (itemLabelName != null) {
            ViewGroup.LayoutParams labelParams = itemLabelName.getLayoutParams();
            labelParams.width = hasFocus ? 0 : width;
            itemLabelName.setLayoutParams(labelParams);
        }
        if (itemLabelNameFocus != null) {
            ViewGroup.LayoutParams focusParams = itemLabelNameFocus.getLayoutParams();
            focusParams.width = hasFocus ? width : 0;
            itemLabelNameFocus.setLayoutParams(focusParams);
        }
        if (itemValue.hasFocus()) {
            itemValue.setTextIsSelectable(true);
            SoftKeyboardHelper.show(getContext(), itemValue);
        }
    }

    public interface RequiredListener {
        void onRequired(String label);
    }
}